package mysql

import (
	"e_mall_server/models"
	"go.uber.org/zap"
	"strings"
)

/**
 * @Author: zhaobin
 * @Description 对表nav进行相关操作
 * @Date: 2022-05-11 22:32
 */

// GetNavList 获取导航列表接口
func GetNavList(query map[string]interface{}) ([]models.Nav, error) {
	var navList []models.Nav
	if err := db.Debug().Where(query).Find(&navList).Error; err != nil {
		return nil, err
	}
	return navList, nil
}

// GetTopNavList 获取顶部导航列表接口
func GetTopNavList() ([]models.Nav, error) {
	var topNavList []models.Nav
	if err := db.Where("status = 1 AND position = 1").Find(&topNavList).Error; err != nil {
		return nil, err
	}
	return topNavList, nil
}

// GetMidNavList 获取中间导航列表接口
func GetMidNavList() ([]models.Nav, error) {
	var midNavList []models.Nav
	db.Where("status = 1 AND position = 2").Find(&midNavList)
	for i := 0; i < len(midNavList); i++ {
		relationIds := strings.Split(midNavList[i].Relation, ",")
		var goodsList []models.Goods
		if err := db.Where("id in ?", relationIds).Select("id,title,goods_img,price").Find(&goodsList).Error; err != nil {
			return nil, err
		}
		midNavList[i].GoodsList = goodsList
	}
	//zap.L().Debug("中间导航列表", zap.Any("value", midNavList))
	return midNavList, nil
}

// AddNav 添加导航信息
func AddNav(nav *models.Nav) error {
	zap.L().Debug("mysql.AddNav()", zap.Any("导航信息", nav))
	// 进行数据库添加操作
	return db.Create(nav).Error
}

// UpdateNav 修改导航信息
func UpdateNav(nav *models.Nav) int64 {
	// 进行数据库修改操作
	value := db.Debug().Model(&nav).Updates(&nav)
	return value.RowsAffected
}

// DeleteNavById 通过导航id删除导航数据
func DeleteNavById(id int64) int64 {
	return db.Delete(&models.Nav{Id: id}).RowsAffected
}

// GetNavDetailById 通过导航id获取导航详细信息
func GetNavDetailById(id int64) (nav models.Nav, err error) {
	nav.Id = id
	err = db.Where("id = ?", id).Find(&nav).Error
	return
}
